//
//  ZYTabBar.m
//  YaYa
//
//  Created by 张亚 on 2019/1/8.
//  Copyright © 2019年 张亚. All rights reserved.
//

#import "ZYTabBar.h"

@interface ZYTabBar ()

@property (nonatomic, strong) UIView * tabbgView;
@property (nonatomic, strong) NSArray * datalist;
@property (nonatomic, strong) UIButton * lastItem;
@property (nonatomic, strong) UIButton * cameraButton;

@end

@implementation ZYTabBar


- (NSArray *)datalist {
    if (_datalist == nil) {
        _datalist = @[@"zy1",@"wd1"];
    }
    return _datalist;
}

- (UIView *)tabbgView {
    if (_tabbgView == nil) {
        _tabbgView = [[UIView alloc] init];
        _tabbgView.backgroundColor = [UIColor cyanColor];
    }
    return _tabbgView;
}
- (UIButton *)cameraButton {
    if (_cameraButton == nil) {
        _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraButton setImage:[UIImage imageNamed:@"tjtp"] forState:UIControlStateNormal];
        [_cameraButton sizeToFit];
        _cameraButton.tag = ZYTabBarItemTypeLaunch;
        [_cameraButton addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
//        _cameraButton.backgroundColor = [UIColor purpleColor];
    }
    return _cameraButton;
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //装在背景
        [self addSubview:self.tabbgView];
        
        //装在Item
        for (int i = 0; i < self.datalist.count; i++) {
            UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
            //去除高亮状态
            item.adjustsImageWhenHighlighted = NO;
            [item setImage:[UIImage imageNamed:self.datalist[i]] forState:UIControlStateNormal];
            [item setImage:[UIImage imageNamed:self.datalist[i]] forState:UIControlStateSelected];
            
            [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
            
            item.tag = ZYTabBarItemTypeLive + i;
            
            if (i == 0) {
                item.selected = YES;
                self.lastItem = item;
            }
            
            [self addSubview:item];
        }
        
        //添加启动直播按钮
        [self addSubview:self.cameraButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.tabbgView.frame = self.bounds;
    
    CGFloat width = self.bounds.size.width / self.datalist.count;
    
    for (int i = 0; i < [self subviews].count; i++) {
        UIView * btn = self.subviews[i];
        if ([btn isKindOfClass:[UIButton class]]) {
            btn.frame = CGRectMake((btn.tag - ZYTabBarItemTypeLive) * width, 0, width, self.frame.size.height);
        }
    }
    
    self.cameraButton.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5 - self.cameraButton.height * 0.5);
    
}

- (void)clickItem:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabbar:clickButton:)]) {
        [self.delegate tabbar:self clickButton:button.tag];
    }
    
    if (self.block) {
        self.block(self, button.tag);
    }
    self.lastItem.selected = NO;
    button.selected = YES;
    self.lastItem = button;
    if (button.tag == ZYTabBarItemTypeLaunch) {
        return;
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        button.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            button.transform = CGAffineTransformIdentity;
        }];
    }];
}

@end
